#! /usr/bin/evn python
# coding:utf-8

from data import DB
from pycalendar import *
import uuid
import  datetime,time

class QComboBox(QComboBox):

    def wheelEvent(self, QWheelEvent):
        pass

class QDateTimeEdit(QDateTimeEdit):

    def wheelEvent(self, QWheelEvent):
        pass

class Measure(QWidget):

    def __init__(self,uuid): #uuid = 身份证
        super(Measure,self).__init__()
        self.setWindowTitle("处置")
        self.setFixedSize(320,280)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.db = DB()
        self.cur = self.db.cur
        self.uuid = uuid
        self.register = 'hsqcdc'
        self.update_mark = None
        self.set_ui()

    def set_ui(self):
        self.save_bnt = QPushButton("保存(F1)")
        self.save_bnt.clicked.connect(self.save_record)
        self.save_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        self.new_bnt = QPushButton("添加(F2)")
        self.new_bnt.clicked.connect(self.new_record)
        self.new_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:brown;border:hidden;")
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.clicked.connect(self.close_win)
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")

        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        bnt_layout.addWidget(self.new_bnt)
        bnt_layout.addWidget(self.save_bnt)
        bnt_widget.setLayout(bnt_layout)

        self.measure_uuid = QLabel(self.gen_uuid())
        self.measure_uuid.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")

        self.test_label = QLabel("检查项目")
        self.test_label.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.test = QComboBox()
        self.test.setEditable(True)
        self.test.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        test_list = ['核酸检测','测温','临床检查','抗体检测','影像检查']
        for i in test_list:
            self.test.addItem(i)
        self.test.setCurrentIndex(0)

        self.result_lable = QLabel("结果")
        self.result_lable.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.result = QLineEdit()
        self.result.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.backup_lable = QLabel("备注")
        self.backup_lable.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.backup= QLineEdit()
        self.backup.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.backup.setPlaceholderText("请输入试剂名称")

        self.regist_date_label = QLabel("操作日期")
        self.regist_date_label.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.regist_date = QDateTimeEdit(QDateTime.currentDateTime())
        self.regist_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.regist_date_bnt = QPushButton()
        self.regist_date_bnt.setStyleSheet("border:hidden;")
        self.regist_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.regist_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.regist_date))

        self.result_date_label = QLabel("结果日期")
        self.result_date_label.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.result_date = QDateTimeEdit(QDateTime.currentDateTime())
        self.result_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.result_date_bnt = QPushButton()
        self.result_date_bnt.setStyleSheet("border:hidden;")
        self.result_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.result_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.result_date))

        layout = QGridLayout()
        layout.addWidget(self.regist_date_label,1,0)
        layout.addWidget(self.regist_date,1,1)
        layout.addWidget(self.regist_date_bnt,1,2)
        layout.addWidget(self.test_label,2,0)
        layout.addWidget(self.test,2,1)
        layout.addWidget(self.result_lable,3,0)
        layout.addWidget(self.result,3,1)
        layout.addWidget(self.result_date_label,4,0)
        layout.addWidget(self.result_date,4,1)
        layout.addWidget(self.result_date_bnt,4,2)
        layout.addWidget(self.backup_lable,7,0)
        layout.addWidget(self.backup,7,1)
        layout.addWidget(bnt_widget,8,0,1,3)
        layout.addWidget(self.measure_uuid,9,0,1,3)

        self.setLayout(layout)

    def save_record(self):
        data_dic = {
            'measure_uuid':self.measure_uuid.text(),
            'test':self.test.currentText(),
            'result':self.result.text(),
            'result_date':self.change_datetime(self.result_date),
            'date':self.change_datetime(self.regist_date),
            'operator':self.register,
            'uuid':self.uuid,
            'backup':self.backup.text()
        }
        if not self.result.text():
            self.messagebox_auto_close('项目未填全')
            return
        if self.update_mark:
            sql = '''update test set
                test = "{test}",result = "{result}",result_date = {result_date},backup="{backup}"
                where test_uuid = "{measure_uuid}"
                '''.format(**data_dic)
        else:
            sql = '''insert into test values (
                        "{measure_uuid}","{uuid}",{date},"{test}",{result_date},"{result}","{backup}","{operator}"
                    )
                    '''.format(**data_dic)
            self.update_mark = 1
            self.save_bnt.setText("更新(F1)")
        print(sql)
        try:
            self.cur.execute(sql)
            self.db.con.commit()
            self.messagebox_auto_close()
        except:
            self.messagebox_auto_close('error')

    def new_record(self):
        self.close()
        self.win = Measure(self.uuid)
        self.win.show()

    def close_win(self):
        self.db.con.close()
        self.close()

    def show_dt_cal(self, tmp):#含时间日历控件
        self.cal = Calendar_With_Clock(tmp)
        self.cal.show()
        self.cal.datetime_signal.connect(self.input_deathday)

    def input_deathday(self, list):
        list[0].setDateTime(list[1])

    def change_datetime(self,pyqtdatetime):   # a pyqtdate style /yyyymmdd to time stamp
        pydate = pyqtdatetime.dateTime().toPyDateTime()
        base_date = datetime.datetime(1970,1,1,0,0)
        day_delta = pydate - base_date
        days = day_delta.days
        seconds = day_delta.seconds
        seconds += days*24*3600
        return seconds

    def gen_uuid(self):
        return (str(uuid.uuid1()))

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def messagebox_auto_close(self,txt = '完成!!!',time =1):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setWindowTitle("# WARNING: ")
        msg.setText(txt)
        msg.setStandardButtons(QMessageBox.Yes)
        msg.button(QMessageBox.Yes).animateClick(time*1000)
        msg.exec_()

class Status(QWidget):

    def __init__(self,uuid):
        super(Status, self).__init__()
        self.setWindowTitle("状态设置")
        self.setFixedSize(300,200)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.uuid = uuid
        self.db = DB()
        self.cur = self.db.cur

        self.cur.execute('select present_status,present_measure from person where uuid = "{}"'.format(self.uuid))
        rslt = self.cur.fetchone()
        if rslt:
            self.present_status = rslt[0]
            self.present_measure =rslt[1]
        else:
            self.present_status = None
            self.present_measure = None
        self.update_mark = None
        self.center()
        self.set_ui()

    def set_ui(self):
        self.save_bnt = QPushButton("保存(F1)")
        self.save_bnt.clicked.connect(self.save_record)
        self.save_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        self.new_bnt = QPushButton("添加(F2)")
        self.new_bnt.clicked.connect(self.new_record)
        self.new_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:brown;border:hidden;")
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.clicked.connect(self.close_win)
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")

        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        bnt_layout.addWidget(self.new_bnt)
        bnt_layout.addWidget(self.save_bnt)
        bnt_widget.setLayout(bnt_layout)

        self.measure_uuid = QLabel(self.gen_uuid())
        self.measure_uuid.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")

        self.regist_date_label = QLabel("操作日期")
        self.regist_date_label.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")
        self.regist_date = QDateTimeEdit(QDateTime.currentDateTime())
        self.regist_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.regist_date_bnt = QPushButton()
        self.regist_date_bnt.setStyleSheet("border:hidden;")
        self.regist_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.regist_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.regist_date))

        self.measure_label = QLabel("处理措施")
        self.measure = QComboBox()
        self.measure.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        test_list2 = ['无措施','居家监测','集中观察','定点医院']
        for i in test_list2:
            self.measure.addItem(i)
        self.measure.setEditable(True)
        if self.present_measure:
            self.measure.setCurrentText(self.present_measure)

        self.status_label = QLabel("目前状态")
        self.status = QComboBox()
        self.status.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        test_list = ['健康人群','一般接触者','密接','初筛阳性','疑似病例','无症状感染者','确诊','康复']
        for i in test_list:
            self.status.addItem(i)
        self.status.setEditable(True)
        if self.present_status:
            self.status.setCurrentText(self.present_status)

        layout = QGridLayout()
        layout.addWidget(self.regist_date_label,1,0)
        layout.addWidget(self.regist_date,1,1,1,2)
        layout.addWidget(self.regist_date_bnt,1,3)
        layout.addWidget(self.status_label,2,0)
        layout.addWidget(self.status,2,1,1,3)
        layout.addWidget(self.measure_label,3,0)
        layout.addWidget(self.measure,3,1,1,3)
        layout.addWidget(bnt_widget,4,0,1,4)
        layout.addWidget(self.measure_uuid,5,0,1,4)

        self.setLayout(layout)

    def save_record(self):
        data_dic = {
            'measure_uuid':self.measure_uuid.text(),
            'date':self.change_datetime(self.regist_date),
            'uuid':self.uuid,
            'status':self.status.currentText(),
            'measure':self.measure.currentText(),
            'operator':'hsqcdc'
        }

        if self.present_measure != data_dic['measure']:
            if not self.update_mark:
                sql = '''insert into measure values (
                "{measure_uuid}","{uuid}",{date},"{measure}","{operator}"
                )
                '''.format(**data_dic)
                self.update_mark = 1
            else:
                sql = '''update measure set date = {date},
                    measure = "{measure}" where measure_uuid = "{measure_uuid}"
                    '''.format(**data_dic)
            self.cur.execute(sql)

        if self.present_status != data_dic['status']:
            if not self.update_mark:
                sql = '''insert into status values ("{measure_uuid}","{uuid}",{date},'{status}')
                '''.format(**data_dic)
                self.update_mark = 1
            else:
                sql = '''update status set date = {date}, status = "{status}" where
                    status_uuid = "{measure_uuid}"'''.format(**data_dic)
            self.cur.execute(sql)

        if True:
            sql = '''select measure_uuid from measure where uuid = "{uuid}" order by date limit 1
                '''.format(**data_dic)
            self.cur.execute(sql)
            try:
                measure_lastest_uuid = self.cur.fetchone()[0]
                if measure_lastest_uuid  == data_dic['measure_uuid']:
                    sql = '''update person set present_measure= "{measure}"
                    '''.format(**data_dic)
                    self.cur.execute(sql)
            except:
                pass
            sql = '''select status_uuid from status where uuid = "{uuid}" order by date limit 1
                '''.format(**data_dic)
            self.cur.execute(sql)
            try:
                status_lastest_uuid = self.cur.fetchone()[0]
                if status_lastest_uuid  == data_dic['measure_uuid']:
                    sql = '''update person set present_status = "{status}"
                        '''.format(**data_dic)
                    self.cur.execute(sql)
            except:
                pass
        self.db.con.commit()
        self.messagebox_auto_close()

    def new_record(self):
        self.measure.setCurrentIndex(0)
        self.status.setCurrentIndex(0)
        self.measure_uuid.setText(self.gen_uuid())

    def close_win(self):
        self.db.con.close()
        self.close()

    def show_dt_cal(self, tmp):#含时间日历控件
        self.cal = Calendar_With_Clock(tmp)
        self.cal.show()
        self.cal.datetime_signal.connect(self.input_deathday)

    def input_deathday(self, list):
        list[0].setDateTime(list[1])

    def change_datetime(self,pyqtdatetime):   # a pyqtdate style /yyyymmdd to time stamp
        pydate = pyqtdatetime.dateTime().toPyDateTime()
        base_date = datetime.datetime(1970,1,1,0,0)
        day_delta = pydate - base_date
        days = day_delta.days
        seconds = day_delta.seconds
        seconds += days*24*3600
        return seconds

    def gen_uuid(self):
        return (str(uuid.uuid1()))

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()
        if e.key() == Qt.Key_F1:
            self.save_record()
        if e.key() == Qt.Key_F2:
            self.new_record()

    def messagebox_auto_close(self,txt = '完成!!!',time =1):
        msg = QMessageBox()
        msg.setIcon(QMessageBox.Information)
        msg.setWindowTitle("# WARNING: ")
        msg.setText(txt)
        msg.setStandardButtons(QMessageBox.Yes)
        msg.button(QMessageBox.Yes).animateClick(time*1000)
        msg.exec_()


class Measure_History(QWidget):

    def __init__(self,uuid):
        super(Measure_History,self).__init__()
        self.uuid = uuid
        self.setWindowTitle("处置一览表")
        self.setFixedSize(650,400)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.win = None
        self.db = DB()
        self.cur = self.db.cur
        self.set_ui()

    def set_ui(self):

        new_bnt = QPushButton("新增")
        new_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        new_bnt.clicked.connect(self.add_new)
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")
        self.close_bnt.clicked.connect(self.close_win)
        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        # bnt_layout.addWidget(new_bnt)
        bnt_widget.setLayout(bnt_layout)

        self.measure_table = QTableWidget(5,2)
        self.status_table = QTableWidget(5,2)
        self.test_table = QTableWidget(5,5)
        self.measure_table.setShowGrid(False)
        self.measure_table.setFrameShape(QFrame.NoFrame)
        self.measure_table.setStyleSheet('background:transparent;')
        self.status_table.setShowGrid(False)
        self.status_table.setFrameShape(QFrame.NoFrame)
        self.status_table.setStyleSheet('background:transparent;')
        self.test_table.setShowGrid(False)
        self.test_table.setFrameShape(QFrame.NoFrame)
        self.test_table.setStyleSheet('background:transparent;')

        layout = QGridLayout()
        # layout.setColumnStretch(0,450)
        # layout.setColumnStretch(1,350)
        layout.addWidget(QLabel(">>>>>检查-------------"),0,0,1,6)
        layout.addWidget(self.test_table,1,0,5,6)
        layout.addWidget(QLabel(">>>>>状态-------------"),6,0)
        layout.addWidget(self.status_table,7,0,3,3)
        layout.addWidget(QLabel(">>>>>处置-------------"),6,3)
        layout.addWidget(self.measure_table,7,3,3,3)
        # layout.addWidget(new_bnt,4,1)
        layout.addWidget(bnt_widget,10,0,1,6)
        self.setLayout(layout)

    def refresh_table(self):
        self.db2 = DB()
        self.cur2 = self.db2.cur
        self.measure_table.clear()
        self.status_table.clear()
        self.test_table.clear()
        self.measure_table.setHorizontalHeaderLabels(['时间','措施'])
        self.status_table.setHorizontalHeaderLabels(['时间','状态'])
        self.test_table.setHorizontalHeaderLabels(['时间','检查','结果','备注','检测日期'])
        sql = '''select measure_uuid,date,measure from measure where uuid = "{}" order by date desc'''.format(self.uuid)
        self.cur2.execute(sql)
        measure_rslt = self.cur2.fetchall()
        sql = '''select status_uuid,date,status from status where uuid = "{}" order by date desc'''.format(self.uuid)
        self.cur2.execute(sql)
        status_rslt = self.cur2.fetchall()
        sql = '''select test_uuid,date,test,result,backup,result_date from test where uuid = "{}" order by date desc'''.format(self.uuid)
        self.cur2.execute(sql)
        test_rslt = self.cur2.fetchall()
        self.db2.con.close()
        measure_count = len(measure_rslt)
        self.measure_table.setRowCount(measure_count)
        status_count = len(status_rslt)
        self.status_table.setRowCount(status_count)
        test_count = len(test_rslt)
        self.test_table.setRowCount(test_count)
        # measure_table_width_list = [80,120,100]
        for i in range(3):
            self.measure_table.setColumnWidth(i,100)
            self.status_table.setColumnWidth(i,100)
        # measure_table_width_list = [80,100,80,100,80]
        for i in range(6):
            self.test_table.setColumnWidth(i,100)
        k = 0  # k 行数
        for i in measure_rslt:
            if i:
                item_tmp1 = QTableWidgetItem(self.to_date(i[1]))
                item_tmp2 = QTableWidgetItem(str(i[2]))
                item_tmp1.setTextAlignment(Qt.AlignCenter)
                item_tmp2.setTextAlignment(Qt.AlignCenter)
                self.measure_table.setItem(k,0,item_tmp1)
                self.measure_table.setItem(k,1,item_tmp2)
                # self.measure_table.setCellWidget(k,2,self.button_row(i[0],1))
            else:
                break
            k += 1
        k = 0
        for i in status_rslt:
            if i:
                item_tmp1 = QTableWidgetItem(self.to_date(i[1]))
                item_tmp2 = QTableWidgetItem(str(i[2]))
                item_tmp1.setTextAlignment(Qt.AlignCenter)
                item_tmp2.setTextAlignment(Qt.AlignCenter)
                self.status_table.setItem(k,0,item_tmp1)
                self.status_table.setItem(k,1,item_tmp2)
                # self.status_table.setCellWidget(k,2,self.button_row(i[0],2))
            else:
                break
            k += 1
        k = 0
        for i in test_rslt:
            if i:
                item_tmp1 = QTableWidgetItem(self.to_date(i[1]))
                item_tmp2 = QTableWidgetItem(str(i[2]))
                item_tmp3 = QTableWidgetItem(str(i[3]))
                item_tmp4 = QTableWidgetItem(str(i[4]))
                item_tmp5 = QTableWidgetItem(self.to_date(i[5]))
                item_tmp1.setTextAlignment(Qt.AlignCenter)
                item_tmp2.setTextAlignment(Qt.AlignCenter)
                item_tmp3.setTextAlignment(Qt.AlignCenter)
                item_tmp4.setTextAlignment(Qt.AlignCenter)
                item_tmp5.setTextAlignment(Qt.AlignCenter)
                self.test_table.setItem(k,0,item_tmp1)
                self.test_table.setItem(k,1,item_tmp2)
                self.test_table.setItem(k,2,item_tmp3)
                self.test_table.setItem(k,3,item_tmp4)
                self.test_table.setItem(k,4,item_tmp5)
                # self.test_table.setCellWidget(k,5,self.button_row(i[0],0))
            else:
                break
            k += 1

    def button_row(self, tmp_uuid,mark):
        self.widget = QWidget()
        self.view_bnt = QPushButton('查看')
        self.del_bnt = QPushButton('删除')

        self.view_bnt.setStyleSheet('''text-align:center; background-color:green;border-style:outset;height:20px;color:white;''')
        self.del_bnt.setStyleSheet('''text-align:center;background-color: red;border-style:outset;height:20px;color:white;''')

        self.hlayout = QHBoxLayout()

        self.hlayout.addWidget(self.view_bnt)
        self.hlayout.addWidget(self.del_bnt)
        self.hlayout.setContentsMargins(5,2,5,2)

        self.view_bnt.clicked.connect(lambda:self.view_record(tmp_uuid,mark))
        self.del_bnt.clicked.connect(lambda:self.del_record(tmp_uuid,mark))
        self.widget.setLayout(self.hlayout)
        return self.widget

    def view_record(self,tmp_uuid,mark):
        if self.win:
            self.win.close()
        self.win = Measure(tmp_uuid)
        self.cur.execute('select * from test where test_uuid = "{}"'.format(tmp_uuid))
        test_rslt = self.cur.fetchone()
        self.cur.execute('select * from status where status_uuid = "{}"'.format(tmp_uuid))
        status_rslt = self.cur.fetchone()
        self.cur.execute('select * from measure where measure_uuid = "{}"'.format(tmp_uuid))
        measure_rslt = self.cur.fetchone()
        self.win.uuid = self.uuid
        self.win.update_mark = True
        if mark == 1:
            self.win.choose_measure()
            self.win.measure_uuid.setText(measure_rslt[0])
            self.win.regist_date.setDateTime(self.to_pydatetime(measure_rslt[2]))
            self.win.measure.setCurrentText(measure_rslt[3])
        elif mark == 2:
            self.win.choose_measure()
            self.win.measure_uuid.setText(status_rslt[0])
            self.win.regist_date.setDateTime(self.to_pydatetime(status_rslt[2]))
            self.win.status.setCurrentText(status_rslt[3])
        else:
            self.win.choose_test()
            self.win.measure_uuid.setText(test_rslt[0])
            self.win.regist_date.setDateTime(self.to_pydatetime(test_rslt[2]))
            self.win.test.setCurrentText(test_rslt[3])
            self.win.result.setText(test_rslt[4])
            self.win.result_date.setDateTime(self.to_pydatetime(test_rslt[5]))
        self.win.show()
        self.win.new_bnt.setVisible(False)
        self.win.close_bnt.clicked.connect(self.refresh)

    def del_record(self,tmp_uuid,mark):
        if mark == 1:
            sql = ''' delete from measure where measure_uuid = "{}"
                '''.format(tmp_uuid)
        elif mark == 2:
            sql = ''' delete from status where status_uuid = "{}"
                '''.format(tmp_uuid)
        else:
            sql = ''' delete from test where test_uuid = "{}"
                '''.format(tmp_uuid)
        msg = QMessageBox.warning(self,'tips','是否删除',QMessageBox.Yes,QMessageBox.No)
        if msg == QMessageBox.Yes:
            self.cur.execute(sql)
            self.db.con.commit()
            self.refresh()

    def to_date(self,a):
        b = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=a)
        c = b.strftime('%m/%d %H:%M')
        return c

    def to_pydatetime(self, a):
        b = datetime.datetime(1970, 1, 1, 0, 0) + datetime.timedelta(seconds=a)
        return QDateTime(b.year, b.month, b.day, b.hour, b.minute)

    def close_win(self):
        self.db.con.close()
        self.close()

    def refresh(self):
        self.refresh_table()

    def add_new(self):
        if self.win:
            self.win.close()
        self.win = Measure(self.uuid)
        self.win.show()
        self.win.close_bnt.clicked.connect(self.refresh)

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./img/barcode.ico'))
    win = Status('341003199909011234')
    win.show()
    sys.exit(app.exec_())
